/**
 * Created by Administrator on 2016-07-31.domReady.js
 * DataTables
 */

define([//3.加载依赖的库
        'jquery',
        'domReady!',
        'bootstrap',
        'datatables.net-bs',
        'datatables.net-select',
        'datatables.net-scroller',
        'jstree'
    ],
    function ($) {
        var impContent = function () {
            $.get("edit-modal.html", {}, function (content) {
                $("#body-container").append(content);
            });

            $.get("error-modal.html", {}, function (content) {
                $("#body-container").append(content);
            });

            $.get("delete-modal.html", {}, function (content) {
                $("#body-container").append(content);
            });
        }

        var searchCond;
        var reportTable;

        var initTree = function () {
            // $('#jstree_rpttype').data('jstree', false).empty();
            $('#jstree_rpttype')
                .jstree({
                    "core": {
                        "animation": 0,
                        "check_callback": true,
                        'force_text': true,
                        "themes": {"stripes": true},
                        'data': {
                            "dataType": "json",
                            'url': "/agcfs/report/getRptType",
                            'success': function (data) {
                                // initFldTable();
                                initDataTable();
                            },
                            // 'async': false,
                            'cache': false,
                            'data': function (node) {
                                return {'id': node.id};
                            }
                        }
                    },
                    "types": {
                        "#": {"max_children": 1, "max_depth": 4, "valid_children": ["root"]},
                        "root": {"valid_children": ["default"]},
                        "default": {"valid_children": ["default", "file"]},
                        "file": {"icon": "glyphicon glyphicon-file", "valid_children": []}
                    },
                    "plugins": ["contextmenu", "dnd", "search", "state", "types", "wholerow"]
                }).on('changed.jstree', function (e, data) {
                    var nodeData = data.instance.get_node(data.selected[0]).original;
                    if (typeof(nodeData) != "undefined") {
                        searchCond = JSON.stringify({
                            nd:nodeData.nd,
                            reportType: nodeData.code
                        });
                        refreshRptTable();
                        // $('#reportlist').DataTable().ajax.reload(null,true);
                    }
                });
        }

        var initDataTable = function () {
            reportTable = $('#reportlist').DataTable({
                lengthChange: false,
                searching:false,
                paging: true,
                ajax: {
                    "url": "/agcfs/report/getReportList",
                    "data": function (data) {
                        //添加额外的参数传给服务器
                        data.extra_search = searchCond;
                    }
                },
                serverSide: false,
                bPaginate: false,
                scrollX: true,
                scrollY: true,
                rowid: "id",
                columns: [
                    {
                        data: null,
                        defaultContent: '',
                        className: 'select-checkbox',
                        orderable: false
                    },
                    {data: "nd"},
                    {data: "sequNo"},
                    {data: "reportCode"},
                    {data: "reportName"},
                    {data: "reportTypeName"}
                ],
                order: [ 1, 'asc' ],

                select: {
                    style: 'multi', /* os,single,multi, multi+shift,api*/
                    selector: 'td.select-checkbox'
                },
                language: {
                    "lengthMenu": "每页显示 _MENU_ 条记录",
                    "info": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                    "infoEmpty": "没有数据",
                    "infoFiltered": "(从 _MAX_ 条数据中检索)",
                    "zeroRecords": "没有检索到数据",
                    "search": "搜索:",
                    "paginate": {
                        "first": "首页",
                        "previous": "前一页",
                        "next": "后一页",
                        "last": "尾页"
                    },
                    select: {
                        rows: {
                            _: "%d行被选中",
                            0: ""
                        }
                    }
                }
            });
        }

   

        var initFldTable = function () {
            var dd = $('#rptFldlist').DataTable({
                lengthChange: false,
                searching:false,
                paging: true,
                ajax: {
                    "url": "/agcfs/report/getRptFldList",
                    "data": function (data) {
                        //添加额外的参数传给服务器
                        data.extra_search = JSON.stringify({
                            nd:'2016',
                            rptCode: 'test1'
                        });
                    }
                },
                serverSide: false,
                bPaginate: false,
                scrollX: true,
                scrollY: true,
                rowid: "id",
                columns: [
                    {
                        data: null,
                        defaultContent: '',
                        className: 'select-checkbox',
                        orderable: false
                    },
                    {data: "nd"},
                    {data: "sequNo"},
                    {data: "reportCode"},
                    {data: "reportName"},
                    {data: "reportTypeName"}
                ],
                order: [ 1, 'asc' ],

                select: {
                    style: 'multi', /* os,single,multi, multi+shift,api*/
                    selector: 'td.select-checkbox'
                },
                language: {
                    "lengthMenu": "每页显示 _MENU_ 条记录",
                    "info": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                    "infoEmpty": "没有数据",
                    "infoFiltered": "(从 _MAX_ 条数据中检索)",
                    "zeroRecords": "没有检索到数据",
                    "search": "搜索:",
                    "paginate": {
                        "first": "首页",
                        "previous": "前一页",
                        "next": "后一页",
                        "last": "尾页"
                    },
                    select: {
                        rows: {
                            _: "%d行被选中",
                            0: ""
                        }
                    }
                }
            });
        }

        var initEditNd = function() {
            var editNd = "#report-nd";
            $.ajax({
                url: "/agcfs/baseData/bookType/getAllNd",
                type: "GET",
                dataType: "json",
                data: {},
                success: function (data) {
                    $(editNd).css("color", "");
                    $(editNd).html("");
                    for (var i = 0; i < data.length; i++) {
                        var temp = '<option>' + data[i] + '</option>';
                        $(editNd).append(temp);
                    }
                }
            });
        }

        var myDT = {};
        myDT.init = function () {
            impContent();
            initTree();
            initFldTable();
            initEditNd();
        };
        require(myDT.init());

        return {
            'myDT': myDT
        };
    }

);

var refreshRptTable = function () {
    $('#reportlist').DataTable().ajax.reload(null,true);
}


